package org.xtlgen;

import java.awt.geom.Rectangle2D;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Element;

/**
 * Represents the complete DrawingArea in the XtlGen system.
 * It is used as the root Shape and can optionally specify a scale and
 * the origin coordinates for display.
 * <p>
 * A DrawingArea must be specified like this:
 * </p>
 * <pre>
 * &lt;DrawingArea width="[width] height="[height]" [xorigin="[xorigin]" yorigin="[yorigin]" scale="[scale]"]&gt;
 *   [subshapes]
 * &lt;/DrawingArea&gt;
 * </pre>
 * in the configuration file.
 */
public class DrawingArea extends ClosedShape {
  /**
   * Construct a DrawingArea.
   *
   * @param params  parameters for this Shape as read from the config file
   *
   * @throws IllegalArgumentException if one of the required parameters cannot
   *         be found on the given params object.
   */
  public DrawingArea(Element params) {
    super(null, params);
    
    this.scale = 1.0;
    this.xOrigin = 0.0;
    this.yOrigin = 0.0;

    ShapeAttributes shapeAttributes = new ShapeAttributes(params.getAttributes());
    
    // Mandatory attributes
    try {
      this.width = shapeAttributes.getDouble("width");
      this.height = shapeAttributes.getDouble("height");
    }
    catch(IllegalArgumentException e) {
      throw new IllegalArgumentException("DrawingArea: " + e.getMessage());
    }
      
    // Optional attributes
    try {
      this.xOrigin = shapeAttributes.getDouble("xorigin");
    }
    catch(IllegalArgumentException e) {
      // Swallow since attribute is optional
    }
    
    try {
      this.yOrigin = shapeAttributes.getDouble("yorigin");
    }
    catch(IllegalArgumentException e) {
      // Swallow since attribute is optional
    }
    
    try {
      this.scale = shapeAttributes.getDouble("scale");
    }
    catch(IllegalArgumentException e) {
      // Swallow since attribute is optional
    }
  }

  /**
   * Return a null AWT Shape.
   *
   * @param params  parameters for this Shape as read from the config file
   *
   * @return null.
   */
  protected java.awt.Shape createAWTShape(Element params) {
    return null;
  }

  /**
   * Display children Shapes on the given canvas object.
   *
   * @param canvas  the target canvas
   *
   * @see #drawChildren
   */
  public void draw(Canvas canvas) {
    // Draw children
    drawChildren(canvas);
  }

  /**
   * Get the list of child nodes in params.
   *
   * @return The child node list.
   */
  protected NodeList getChildren(Element params) {
    return params.getChildNodes();
  }

  /**
   * Get the scale used for displaying this drawing area
   *
   * @return The scale.
   */
  public double getScale() {
    return this.scale;
  }
  
  /**
   * Get the origin X coordinate used for displaying this drawing area
   *
   * @return The origin X coordinate.
   */
  public double getXOrigin() {
    return this.xOrigin;
  }
  
  /**
   * Get the origin Y coordinate used for displaying this drawing area
   *
   * @return The origin Y coordinate.
   */
  public double getYOrigin() {
    return this.yOrigin;
  }
  
  /**
   * Get the width used for displaying this drawing area
   *
   * @return The width.
   */
  public double getWidth() {
    return this.width;
  }
  
  /**
   * Get the height used for displaying this drawing area
   *
   * @return The height.
   */
  public double getHeight() {
    return this.height;
  }
  
  /*
   * Fields
   */
  double scale;
  double xOrigin;
  double yOrigin;
  double width;
  double height;
}